package com.ming;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class GameWin extends JFrame {

    //0 未开始 1 运行中 2 商店 3 失败 4 成功
    static  int state;
    //存储金块、石块
    List<Object> objectList = new ArrayList<>();

    Bg bg = new Bg();
    Line line = new Line(this);
    //定义画布
    Image offScreenImage;

    {
        //是否可以放置
        boolean isPlace = true;
        for (int i = 0; i < 8; i++) {
            double random = Math.random();
            Gold gold;//存放当前生成的金块
            if (random < 0.3){ gold = new GoldMini();}
            else if (random < 0.7){gold = new Gold();}
            else{gold = new GoldPlus();}

            for (Object obj:objectList) {
                if (gold.getRec().intersects(obj.getRec())){
                    //发生重叠，当前位置不可放置
                    isPlace = false;
                }
            }
            if (isPlace){
                objectList.add(gold);
            }else{
                isPlace = true;
                i--;
            }
        }
        for (int i = 0; i < 3; i++) {
            Rock rock = new Rock();
            for (Object obj:objectList) {
                if (rock.getRec().intersects(obj.getRec())){
                    //发生重叠，当前位置不可放置
                    isPlace = false;
                }
            }
            if (isPlace){
                objectList.add(rock);
            }else{
                isPlace = true;
                i--;
            }
        }
    }

    public static void main(String[] args) {
        GameWin  gameWin = new GameWin();
        gameWin.launch();
    }

    void launch(){
        this.setVisible(true);
        this.setSize(768,1000);
        this.setTitle("黄金矿工");
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //鼠标事件
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                switch (state){
                    case 0:
                        bg.startTime = System.currentTimeMillis();
                        if (e.getButton()== 3){
                            state = 1;
                        }
                        break;
                    case 1:
                        //线左右变换
                        if (e.getButton()== 1 && line.status == 0){
                            line.status = 1;
                        }else if (e.getButton()== 3 && line.status == 3 && Bg.waterNum > 0){
                            //line.status = 2;
                            Bg.waterFlag = true;
                            Bg.waterNum--;
                        }
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                }


            }
        });

        while (true){
            repaint();
            nextLevel();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void nextLevel(){
        if (bg.gameTime() && state == 1){
            if (Bg.score >= bg.goal){
                Bg.level++;
                dispose();
                GameWin gameWin1 = new GameWin();
                gameWin1.launch();
            }else {
                state = 3;
            }
        }
    }

    @Override
    public void paint(Graphics g) {
       offScreenImage  = this.createImage(768,1000);
       Graphics gImage = offScreenImage.getGraphics();

       bg.paintSelf(gImage);

       if (state == 1){
           for(Object obj:objectList){
               obj.paintSelf(gImage);
           }

           line.paintSelf(gImage);
       }

       g.drawImage(offScreenImage,0,0,null);
    }



}
